﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsCanTru
    {
        private string mDienGiai = "";
        private string mDienGiaiKhac = "";
        private string mKhoa = "";
        private string mKhoaChungTuCanTru = "";
        private string mKhoaDoiTuongCanTru = "";
        private string mKhoaTaiKhoanNo = "";
        private string mLoaiCanTru = "";
        private string mLoaiChungTu = "";
        private string mLoaiTien = "";
        private string mNgayChungTu = "";
        private string mNgayChungTuCanTru = "";
        private string mNgayHoaDonCanTru = "";
        private string mNguonNhap = "";
        private string mSoChungTu = "";
        private string mSoChungTuCanTru = "";
        private string mSoHoaDonCanTru = "";
        private double mSoTienChungTu = 0.0;
        private double mSoTienChungTuNT = 0.0;
        private double mSoTienConLai = 0.0;
        private double mSoTienConLaiNT = 0.0;
        private double mSoTienLe = 0.0;
        private double mSoTienLeNT = 0.0;
        private double mTyGia = 0.0;

        public void ClearTemp(string pKeyTable)
        {
            string commandText = "DELETE FROM GL_CanTruChiTietTmp WHERE KhoaCanTru = '" + pKeyTable.Trim() + "'";
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
        }

        public bool DelData(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, pKhoa);
                commandParameters[1] = DBHelper.createParameter("@pError", DbType.Int64, ParameterDirection.Output, 0);
                bool flag2 = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_CanTruDel", commandParameters) > 0;
                flag = flag2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetList(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT CTR.KHOA, CTR.SOCHUNGTU, dbo.Char2Date(CTR.NGAYCHUNGTU) NGAYCHUNGTU,         CASE CTR.LOAICANTRU         WHEN 'A/P' THEN  N'Chứng từ mua h\x00e0ng'         WHEN 'A/R(-)' THEN N'Chứng từ thu tiền thừa'         ELSE N'Kh\x00e1ch h\x00e0ng ứng trước' END LOAICANTRU,         TK.SOTAIKHOAN, TK.TENVIET TENTAIKHOAN, CTR.SOCHUNGTUCANTRU, dbo.Char2Date(CTR.NGAYCHUNGTUCANTRU) NGAYCHUNGTUCANTRU,         DT.TENVIET TENDOITUONG, CTR.LOAITIEN, CTR.TYGIA, CTR.SOTIENCHUNGTUNT SOTIENGIAODICH,         CTR.SOTIENCHUNGTU SOTIENGHISO, CTR.SOTIENCONLAI, CTR.SOTIENCONLAI, CTR.DIENGIAI, CTR.DIENGIAIKHAC FROM GL_CANTRU CTR LEFT JOIN DM_TAIKHOAN TK ON CTR.KHOATAIKHOANNO = TK.KHOA LEFT JOIN DM_DOITUONG DT ON CTR.KHOADOITUONGCANTRU = DT.KHOA " + str2;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool IsDuplicateVoucherNo(string strVoucherNo, string strKeyTable)
        {
            string commandText = "SELECT * FROM GL_CanTru Where Rtrim(SoChungTu) = '" + strVoucherNo.Trim() + "' AND Khoa <> '" + strKeyTable + "'";
            return (DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0].Rows.Count > 0);
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            try
            {
                string commandText = "SELECT * FROM GL_CanTru WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mSoChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mNgayChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mLoaiCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mNguonNhap = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mKhoaTaiKhoanNo = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mKhoaChungTuCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mSoChungTuCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetString));
                    this.mNgayChungTuCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetString));
                    this.mSoHoaDonCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetString));
                    this.mNgayHoaDonCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDoiTuongCanTru = Conversions.ToString(modGeneral.GetValue(dataReader, 11, modGeneral.typeOfColumn.GetString));
                    this.mLoaiTien = Conversions.ToString(modGeneral.GetValue(dataReader, 12, modGeneral.typeOfColumn.GetString));
                    this.mTyGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 13, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienChungTuNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 14, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienChungTu = Conversions.ToDouble(modGeneral.GetValue(dataReader, 15, modGeneral.typeOfColumn.GetDecimal));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 0x10, modGeneral.typeOfColumn.GetString));
                    this.mDienGiaiKhac = Conversions.ToString(modGeneral.GetValue(dataReader, 0x11, modGeneral.typeOfColumn.GetString));
                    this.mLoaiChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 0x12, modGeneral.typeOfColumn.GetString));
                    this.mSoTienConLaiNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 0x13, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienConLai = Conversions.ToDouble(modGeneral.GetValue(dataReader, 20, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienLeNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 0x15, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTienLe = Conversions.ToDouble(modGeneral.GetValue(dataReader, 0x16, modGeneral.typeOfColumn.GetDecimal));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[0x18];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@SoChungTu", DbType.Int64, ParameterDirection.Input, this.mSoChungTu);
                commandParameters[2] = DBHelper.createParameter("@NgayChungTu", DbType.Int64, ParameterDirection.Input, this.mNgayChungTu);
                commandParameters[3] = DBHelper.createParameter("@LoaiCanTru", DbType.Int64, ParameterDirection.Input, this.mLoaiCanTru);
                commandParameters[4] = DBHelper.createParameter("@NguonNhap", DbType.Int64, ParameterDirection.Input, this.mNguonNhap);
                commandParameters[5] = DBHelper.createParameter("@KhoaTaiKhoanNo", DbType.Int64, ParameterDirection.Input, this.mKhoaTaiKhoanNo);
                commandParameters[6] = DBHelper.createParameter("@KhoaChungTuCanTru", DbType.Int64, ParameterDirection.Input, this.mKhoaChungTuCanTru);
                commandParameters[7] = DBHelper.createParameter("@SoChungTuCanTru", DbType.Int64, ParameterDirection.Input, this.mSoChungTuCanTru);
                commandParameters[8] = DBHelper.createParameter("@NgayChungTuCanTru", DbType.Int64, ParameterDirection.Input, this.mNgayChungTuCanTru);
                commandParameters[9] = DBHelper.createParameter("@SoHoaDonCanTru", DbType.Int64, ParameterDirection.Input, this.mSoHoaDonCanTru);
                commandParameters[10] = DBHelper.createParameter("@NgayHoaDonCanTru", DbType.Int64, ParameterDirection.Input, this.mNgayHoaDonCanTru);
                commandParameters[11] = DBHelper.createParameter("@KhoaDoiTuongCanTru", DbType.Int64, ParameterDirection.Input, this.mKhoaDoiTuongCanTru);
                commandParameters[12] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[13] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[14] = DBHelper.createParameter("@SoTienChungTuNT", DbType.Date, ParameterDirection.Input, this.mSoTienChungTuNT);
                commandParameters[15] = DBHelper.createParameter("@SoTienChungTu", DbType.Date, ParameterDirection.Input, this.mSoTienChungTu);
                commandParameters[0x10] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, this.mDienGiai);
                commandParameters[0x11] = DBHelper.createParameter("@DienGiaiKhac", DbType.Int64, ParameterDirection.Input, this.mDienGiaiKhac);
                commandParameters[0x12] = DBHelper.createParameter("@LoaiChungTu", DbType.Int64, ParameterDirection.Input, this.mLoaiChungTu);
                commandParameters[0x13] = DBHelper.createParameter("@SoTienConLaiNT", DbType.Date, ParameterDirection.Input, this.mSoTienConLaiNT);
                commandParameters[20] = DBHelper.createParameter("@SoTienConLai", DbType.Date, ParameterDirection.Input, this.mSoTienConLai);
                commandParameters[0x15] = DBHelper.createParameter("@SoTienLeNT", DbType.Date, ParameterDirection.Input, this.mSoTienLeNT);
                commandParameters[0x16] = DBHelper.createParameter("@SoTienLe", DbType.Date, ParameterDirection.Input, this.mSoTienLe);
                commandParameters[0x17] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Input, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_CanTru", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(commandParameters[0x17].Value, 0, false))
                {
                    return true;
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public string DienGiaiKhac
        {
            get
            {
                return this.mDienGiaiKhac;
            }
            set
            {
                this.mDienGiaiKhac = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaChungTuCanTru
        {
            get
            {
                return this.mKhoaChungTuCanTru;
            }
            set
            {
                this.mKhoaChungTuCanTru = value;
            }
        }

        public string KhoaDoiTuongCanTru
        {
            get
            {
                return this.mKhoaDoiTuongCanTru;
            }
            set
            {
                this.mKhoaDoiTuongCanTru = value;
            }
        }

        public string KhoaTaiKhoanNo
        {
            get
            {
                return this.mKhoaTaiKhoanNo;
            }
            set
            {
                this.mKhoaTaiKhoanNo = value;
            }
        }

        public string LoaiCanTru
        {
            get
            {
                return this.mLoaiCanTru;
            }
            set
            {
                this.mLoaiCanTru = value;
            }
        }

        public string LoaiChungTu
        {
            get
            {
                return this.mLoaiChungTu;
            }
            set
            {
                this.mLoaiChungTu = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NgayChungTu
        {
            get
            {
                return this.mNgayChungTu;
            }
            set
            {
                this.mNgayChungTu = value;
            }
        }

        public string NgayChungTuCanTru
        {
            get
            {
                return this.mNgayChungTuCanTru;
            }
            set
            {
                this.mNgayChungTuCanTru = value;
            }
        }

        public string NgayHoaDonCanTru
        {
            get
            {
                return this.mNgayHoaDonCanTru;
            }
            set
            {
                this.mNgayHoaDonCanTru = value;
            }
        }

        public string NguonNhap
        {
            get
            {
                return this.mNguonNhap;
            }
            set
            {
                this.mNguonNhap = value;
            }
        }

        public string SoChungTu
        {
            get
            {
                return this.mSoChungTu;
            }
            set
            {
                this.mSoChungTu = value;
            }
        }

        public string SoChungTuCanTru
        {
            get
            {
                return this.mSoChungTuCanTru;
            }
            set
            {
                this.mSoChungTuCanTru = value;
            }
        }

        public string SoHoaDonCanTru
        {
            get
            {
                return this.mSoHoaDonCanTru;
            }
            set
            {
                this.mSoHoaDonCanTru = value;
            }
        }

        public double SoTienChungTu
        {
            get
            {
                return this.mSoTienChungTu;
            }
            set
            {
                this.mSoTienChungTu = value;
            }
        }

        public double SoTienChungTuNT
        {
            get
            {
                return this.mSoTienChungTuNT;
            }
            set
            {
                this.mSoTienChungTuNT = value;
            }
        }

        public double SoTienConLai
        {
            get
            {
                return this.mSoTienConLai;
            }
            set
            {
                this.mSoTienConLai = value;
            }
        }

        public double SoTienConLaiNT
        {
            get
            {
                return this.mSoTienConLaiNT;
            }
            set
            {
                this.mSoTienConLaiNT = value;
            }
        }

        public double SoTienLe
        {
            get
            {
                return this.mSoTienLe;
            }
            set
            {
                this.mSoTienLe = value;
            }
        }

        public double SoTienLeNT
        {
            get
            {
                return this.mSoTienLeNT;
            }
            set
            {
                this.mSoTienLeNT = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

